package com.salesmonitoring.controller;

import java.awt.Color;
import java.io.FileOutputStream;
import java.util.ArrayList;

import com.lowagie.text.Anchor;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfMediaClipData;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTable;
import com.lowagie.text.pdf.PdfWriter;
import com.salesmonitoring.model.Transaction;

public class Exporter {

	private ArrayList<Transaction> reports;
	private String[] titles;
	private String reportTitle;
	
	public Exporter(ArrayList<Transaction> reports, String[] titles) {
		this.reports = reports;
		this.titles = titles;
	}
	
	public void setReportTitle(String officer, String month, String year) {
		
		month = getMonth(month);
		
		if(!officer.equals("") && !month.equals("") && !year.equals("")) {
			reportTitle = officer + "'s Sales as of " + month + " " + year;
		} else if(!officer.equals("") && month.equals("") && !year.equals("")) {
			reportTitle = officer + "'s Sales as of " + year;
		} else if(!officer.equals("") && !month.equals("") && year.equals("")) {
			reportTitle = officer + "'s Sales as of " + month;
		}else if(officer.equals("") && !month.equals("") && !year.equals("")) {
			reportTitle = "Sales Report for " + month + " " + year;
		} else if(officer.equals("") && month.equals("") && !year.equals("")) {
			reportTitle = "Sales Report for " + year;
		} else if(!officer.equals("") && month.equals("") && year.equals("")) {
			reportTitle = officer + "'s Sales";
		} else if(officer.equals("") && !month.equals("") && year.equals("")) {
			reportTitle = "Sales Report for " + month;
		} else {
			reportTitle = officer + "Sales Report";
		}
		
	}
	
	private String getMonth(String monthNumber) {
		
		String month = "";
		
		if(monthNumber.equals("01")) {
			month = "January";
		} else if(monthNumber.equals("02")) {
			month = "February";
		} else if(monthNumber.equals("03")) {
			month = "March";
		} else if(monthNumber.equals("04")) {
			month = "April";
		} else if(monthNumber.equals("05")) {
			month = "May";
		} else if(monthNumber.equals("06")) {
			month = "June";
		} else if(monthNumber.equals("07")) {
			month = "July";
		} else if(monthNumber.equals("08")) {
			month = "August";
		} else if(monthNumber.equals("09")) {
			month = "September";
		} else if(monthNumber.equals("10")) {
			month = "October";
		} else if(monthNumber.equals("11")) {
			month = "November";
		} else if(monthNumber.equals("12")) {
			month = "December";
		} 
		
		return month;
		
	}
	
	public void export() {
		
		System.out.println("EXPORT DA SHIT.");
		Document document=new Document(PageSize.LEGAL.rotate());
		
		try {
			
			PdfWriter.getInstance(document,new FileOutputStream("C:\\Reports\\NewReport.pdf"));
			
			Font theaderFont = new Font(Font.HELVETICA, 11, Font.BOLD,Color.BLACK);
			Font titleFont = new Font(Font.HELVETICA, 16, Font.BOLD,Color.BLACK);
			
			document.open();
			
			Image bomLogo = Image.getInstance("images//logo.png");
			
			Anchor anchorTarget = new Anchor("\n" + reportTitle, titleFont);
			anchorTarget.setName("BackToTop");
	
			Paragraph paragraph1 = new Paragraph();
			paragraph1.setSpacingBefore(50);
			paragraph1.add(bomLogo);
			paragraph1.add(anchorTarget);
			
			float[] widths = {3.1f,5f,3f,5f,4f,3f,4f,3.8f,3f,3f,2.5f,2f,1.8f,4f};
			PdfPTable table = new PdfPTable(widths);
			table.setWidthPercentage(101f);
			table.setSpacingBefore(25);
			
			for(String s : titles) {
				PdfPCell cell = new PdfPCell(new Phrase(s, theaderFont));
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setVerticalAlignment(Element.ALIGN_CENTER);
				table.addCell(cell);
				
			}
			
			for(Transaction report : reports) {
				table.addCell(report.getDateRequested());
				table.addCell(report.getOldCustomer());
				table.addCell(report.getAccountNumber() + "");
				table.addCell(report.getNewCustomer());
				table.addCell(report.getMotorWarehouse());
				table.addCell(report.getDateSold());
				table.addCell(report.getMotorModel());
				table.addCell(report.getMotorEngine());
				table.addCell(report.getMotorChassis() + "");
				table.addCell(report.getDateRepo());
				table.addCell(report.getMotorPlate());
				table.addCell(report.getAsmf() + "");
				table.addCell(report.getMotorClass());
				table.addCell(report.getRs());
			}
//			
//			for(int y=0; y<reports.size(); y++) {
//				table.addCell(reports.get(y).getDateRequested());
//				table.addCell(reports.get(y).getOldCustomer());
//				table.addCell(reports.get(y).getAccountNumber() + "");
//				table.addCell(reports.get(y).getNewCustomer());
//				table.addCell(reports.get(y).getMotorWarehouse());
//				table.addCell(reports.get(y).getDateSold());
//				table.addCell(reports.get(y).getMotorModel());
//				table.addCell(reports.get(y).getMotorEngine());
//				table.addCell(reports.get(y).getMotorChassis() + "");
//				table.addCell(reports.get(y).getDateRepo());
//				table.addCell(reports.get(y).getMotorPlate());
//				table.addCell(reports.get(y).getAsmf() + "");
//				table.addCell(reports.get(y).getMotorClass());
//				table.addCell(reports.get(y).getRs());
//			
//			}
			
			document.add(paragraph1);
			document.add(table);
			document.close();
			
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
}
